package com.example.mywiseelves.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.InputFilter;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.mywiseelves.R;
import com.example.mywiseelves.adapter.SplashAdapter;
import com.example.mywiseelves.bean.ResourcesBean;
import com.example.mywiseelves.bean.UserBean;
import com.example.mywiseelves.dao.ResourcesDao;
import com.example.mywiseelves.dao.UserDao;
import com.example.mywiseelves.fragment.IllustrateFragment;
import com.example.mywiseelves.utils.PaddingDecoration;

import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 进入设置信息
 * */
public class SetMessagesActivity extends AppCompatActivity implements View.OnClickListener{

    //用户dao对象
    UserDao userDao;
    //资源dao对象
    ResourcesDao resourcesDao;
    //标题栏返回
    ImageView setMessagesBackImageView;
    //设置命名
    EditText setNameEditText;
    Button setNameButton;
    //设置动画图片
    RecyclerView splashRv;
    SplashAdapter splashAdapter;
    List<ResourcesBean>  resourcesBeanList=new ArrayList<>();

    //本地广播管理
    LocalBroadcastManager localBroadcastManager;
    //本地广播,更新数据
    LocalUpDataReceiver localUpDataReceiver;
    //更新过滤器
    IntentFilter intentUpDataFilter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set_messages);
        localBroadcastManager = LocalBroadcastManager.getInstance(this);//忘了
        intentUpDataFilter = new IntentFilter();
        intentUpDataFilter.addAction("updateSplash");
        localUpDataReceiver = new LocalUpDataReceiver();
        localBroadcastManager.registerReceiver(localUpDataReceiver, intentUpDataFilter);
        userDao=new UserDao(this);
        resourcesDao=new ResourcesDao(this);
        initData();
        findViews();
        initViews();

        setMessagesBackImageView.setOnClickListener(this);
        setNameButton.setOnClickListener(this);
    }

    /**
     * 实例化控件
     * */
    private void findViews() {
        setMessagesBackImageView=findViewById(R.id.setMessages_back_iv);
        setNameEditText=findViewById(R.id.setName_et);
        setNameButton=findViewById(R.id.setName_bt);
        splashRv=findViewById(R.id.set_splash_rv);
    }

    /**
     * 初始化化控件
     */
    private void initViews() {
        myFilter(setNameEditText);
        addRecyclerView();
    }

    /**
     * 设置列表控件
     * */
    private void addRecyclerView() {
        //分两列
        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
        splashRv.setLayoutManager(layoutManager);
        splashRv.addItemDecoration(new PaddingDecoration(70));
        //添加适配器，添加数据
        splashAdapter = new SplashAdapter(this,resourcesBeanList);
        splashRv.setAdapter(splashAdapter);
    }

    @Override
    public void onClick(View v) {
        if (v.getId()==R.id.setMessages_back_iv){
            finish();
        }else if (v.getId()==R.id.setName_bt){
            if (setNameEditText.getText().length()<6){
                Toast.makeText(this,"名称不要少于6位",Toast.LENGTH_LONG).show();
            }else if (setNameEditText.getText().length()>8){
                Toast.makeText(this,"名称不超过于8位",Toast.LENGTH_LONG).show();
            }else {
                UserBean userBean=new UserBean();
                userBean.setUser_name(setNameEditText.getText().toString());
                userDao.userPointUpdate4(userBean);
                Toast.makeText(this,"修改成功，"+userBean.getUser_name(),Toast.LENGTH_LONG).show();
            }
        }

    }
    /**
     * 初始化数据
     */
    public void initData() {
        resourcesBeanList=resourcesDao.allSplashQuery();
    }

    /**刷新信息*/
    public void refreshMessages(){
        refreshData();
    }

    /**
     * 刷新数据
     */
    public void refreshData() {
        resourcesBeanList=resourcesDao.allSplashQuery();
        splashAdapter.refreshListData(resourcesBeanList);
    }

    /**
     * 本地广播接收
     */
    private class LocalUpDataReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            refreshMessages();
        }
    }

    /**
     * 指定输入
     */
    public void myFilter(EditText et) {
        InputFilter filter = (source, start, end, dest, dstart, dend) -> {
            for (int i = start; i < end; i++) {
                //判断字母还是数字
                if (!Character.isLetterOrDigit(source.charAt(i))) {
                    return "";
                }
                String speChat = "[\\u4e00-\\u9fa5]";
                //Pattern类的compile方法用于编译一个正则表达式并返回一个编译好的pattern对象
                Pattern pattern = Pattern.compile(speChat);
                //Pattern类的matcher方法用于封装一个要操作的字符串并返回Matcher对象，用于操作字符串
                Matcher matcher = pattern.matcher(source.toString());
                //Matcher类的matches方法用判断字符串和正则表达式是否匹配
                if (!matcher.matches()) {
                    return "";
                } else {
                    return null;
                }
            }
            return null;
        };
        et.setFilters(new InputFilter[]{filter});
    }
}